package com.barbeque.mall.controller;

import com.barbeque.mall.anno.Log;
import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.system.MarketAdmin;
import com.barbeque.mall.bean.system.vo.CreateAdminVO;
import com.barbeque.mall.service.SysAdminServer;
import com.barbeque.mall.util.Constant;
import com.barbeque.mall.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

/**
 * @author 苏东宇
 * @since 2022/07/15 23:26
 */
@RestController
@RequestMapping("admin/admin")
public class SysAdminController {

    @Autowired
    SysAdminServer sysAdminServer;




    /**
     * 显示管理员列表（待修改）
 * @param basePageInfo
     * @return com.barbeque.mall.bean.BaseRespVo
     * @author 苏东宇
     * @since 2022/07/17 20:51
     */
    @RequestMapping("list")
    public BaseRespVo getAdminList(BasePageInfo basePageInfo, String username){

        CommonData adminList;

        adminList = sysAdminServer.getAdminList(basePageInfo, username);

        return BaseRespVo.ok(adminList);
    }







    /**
     * 创建新的管理员
 * @param marketAdmin
     * @return com.barbeque.mall.bean.BaseRespVo
     * @author 苏东宇
     * @since 2022/07/17 20:51
     */
    @Log(action = "创建管理员",op = 2)
    @RequestMapping("create")
    public BaseRespVo createAdmin( @RequestBody  MarketAdmin marketAdmin) throws NoSuchAlgorithmException {
        // if (bindingResult.hasFieldErrors()){
        //     FieldError fieldError = bindingResult.getFieldError();
        //     String msg = fieldError.getField() + fieldError.getRejectedValue();
        //     return BaseRespVo.invalidData(msg);
        // }

        String password = marketAdmin.getPassword();
        String username = marketAdmin.getUsername();
        //根据用户名进行查询，不能注册重复用户名
        List<MarketAdmin> adminByUsername = sysAdminServer.getAdminByUsername(username);
        if (adminByUsername.size() > 0){
            return BaseRespVo.invalidParameter("用户名重复");
        }

        // String currentTime = MyselfTimeUtils.getCurrentTime();
        String md5 = MD5Utils.getMd5(password, Constant.SALT);
        marketAdmin.setPassword(md5);

        //当前时间
        Date date = new Date(System.currentTimeMillis());
        marketAdmin.setUpdateTime(date);
        marketAdmin.setAddTime(date);

        //创建新的管理员
        int admin = sysAdminServer.createAdmin(marketAdmin);
        // //通过用户名查询该管理员信息
        // CreateAdminVO adminByUsername = sysAdminServer.getAdminByUsername(username);
        //添加完成后获取刚刚添加的管理员的id
        Integer id = marketAdmin.getId();
        CreateAdminVO adminVOById = sysAdminServer.getAdminVOById(id);

        if (admin == 1){
            //此处返回新增管理员信息
            return BaseRespVo.ok(adminVOById);
        }

        return BaseRespVo.invalidData("添加失败");
    }


    /**
     * 更新管理员信息
 * @param marketAdmin
     * @return com.barbeque.mall.bean.BaseRespVo
     * @author 苏东宇
     * @since 2022/07/17 20:51
     */
    @Log(action = "修改管理员信息",op = 2 )
    @RequestMapping("update")
    public BaseRespVo updateAdmin(@RequestBody MarketAdmin marketAdmin) throws NoSuchAlgorithmException {
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        marketAdmin.setUpdateTime(date);
        //将更新的密码进行md5散列
        String password = marketAdmin.getPassword();
        String md5 = MD5Utils.getMd5(password, Constant.SALT);
        //将散列后的密码存入到对象中
        marketAdmin.setPassword(md5);

        int i = sysAdminServer.updateAdmin(marketAdmin);
        if (i == 1){
            return BaseRespVo.ok(marketAdmin);
        }

        return BaseRespVo.invalidData("更新失败");
    }

    /**
     * 删除管理员，逻辑删除，将删除位为置为1
 * @param marketAdmin
     * @return com.barbeque.mall.bean.BaseRespVo
     * @author 苏东宇
     * @since 2022/07/17 20:52
     */
    @Log(action = "删除管理员",op = 2 )
    @RequestMapping("delete")
    public BaseRespVo deleteAdmin(@RequestBody MarketAdmin marketAdmin){
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        marketAdmin.setUpdateTime(date);

        //将删除位为置为1
        marketAdmin.setDeleted(true);

        int i = sysAdminServer.updateAdmin(marketAdmin);
        if (i == 1){
            return BaseRespVo.ok("删除成功");
        }
        return BaseRespVo.invalidData("删除失败");

    }








}
